﻿using BigMonitoringWarningApi.Model.ApiResult;
using GYCTOAManagementSystemNewApi.Controllers.PublicController;
using GYCTOAManagementSystemNewApi.Model;
using GYCTOAManagementSystemNewApi.Model.Request.StaticFile;
using GYCTOAManagementSystemNewApi.Model.Tool;
using GYCTOAManagementSystemNewApi.Public;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace GYCTOAManagementSystemNewApi.Controllers
{
    /// <summary>
    /// 通用文件查询
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class StaticFileController : Controller
    {
        private readonly ILogger<DocumentController> _logger;
        DBContext _db;
        PublicTool _publictool;
        public StaticFileController(ILogger<DocumentController> logger, DBContext db)
        {
            _logger = logger;
            _db = db;
            _publictool = new PublicTool(_db);
        }



        #region 通用文件查询
        /// <summary>
        /// 通用文件查询 
        /// </summary>
        /// <returns></returns>
        [HttpPost("StaticFileByName")]
        public ApiResult StaticFileByName([FromBody] StaticFileByNameRequset staticFile)
        {
            //流程Id
            List<Files> planFiles = PublicTool.GetFileInfo("通用文件", "8192");
            if (staticFile.FileName.Trim()!="")
            {
                planFiles = planFiles.Where(x => x.FileName.Contains(staticFile.FileName)).ToList();
            }
            return ApiResult.True(planFiles);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="url">待删除文件路径</param>
        /// <returns></returns>
        [HttpPost("DeleteFileByUrl")]
        public ApiResult DeleteFileByUrl([FromBody] DeleteFileBypath file)
        {
            try
            {
                var fileloadStr = PublicMethodApiController.FileloadStr;
                var path = AppDomain.CurrentDomain.BaseDirectory;
                var root = Path.GetPathRoot(path);
                string newUrl = file.FilePath.Substring(7, file.FilePath.Length - 7);
                string serverPath = root + fileloadStr + "\\" + "1\\" + newUrl;//1代表OA系统
                PublicTool.DeleteFile(serverPath, false);
                return ApiResult.True();
            }
            catch
            {
                return ApiResult.True("文件删除失败。");
            }
        }
        #endregion





    }
}
